home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8979 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: read/write integers to files
  5. Date: 7 Mar 96 12:42:46 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.826202566@rscernix>
  8. References: <313EBF65.4E82@www.inia.net.au>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <313EBF65.4E82@www.inia.net.au> Jason Collins <jason@www.inia.net.au> writes:
  13.  
  14. >#include <stdio.h>
  15. >#include <stdlib.h>
  16. >
  17. >FILE *filePtr;
  18.  
  19. What's the reason for making filePtr global???
  20.  
  21. >void main()
  22.  ^^^^
  23. Read the FAQ before posting!
  24.  
  25. >{
  26. >  char ctr;
  27. >
  28. >  filePtr=fopen("count.dat", "ab");
  29.  
  30. Open your C book and see what "ab" means.  It's definitely not what you
  31. want in this context!
  32.  
  33. BTW, how do you know the fopen call has succeeded?
  34.  
  35. >  fscanf(filePtr, "%d", &ctr);
  36.  
  37. %d expects a pointer to int and &ctr is a pointer to char.
  38.  
  39. >  fclose(filePtr);
  40. >
  41. >  ctr++;
  42. >
  43. >  filePtr=fopen("count.dat", "wb");
  44.  
  45. Again, "wb" is not what you want here.  And you have no clue whether
  46. fopen succeeded or not!
  47.  
  48. >  fprintf(filePtr, "%d", ctr);
  49.  
  50. Ending every line with a newline character is always a good idea.
  51.  
  52. >  fclose(filePtr);
  53. >}
  54.  
  55. If you want to read/write the integer in binary format (and not in text
  56. format, as you did in this program), check the functions fread/fwrite
  57. in your C book.
  58.  
  59. Dan
  60. --
  61. Dan Pop
  62. CERN, CN Division
  63. Email: danpop@mail.cern.ch 
  64. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  65.